using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

using UMEVOR.DTO;

using Microsoft.Practices.EnterpriseLibrary.Data;

namespace UMEVOR.DAL
{
    public class DALT_AUTOR_DOWNLOAD : DALConnection
    {
        #region Incluir[]

        public void incluir(T_AUTOR_DOWNLOAD[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] insertT_AUTOR_DOWNLOAD = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_AUTOR_DOWNLOAD o = obj[index];
                    SQL = new StringBuilder();

                    SQL.Append(" INSERT INTO T_AUTOR_DOWNLOAD ");
                    SQL.Append("    (ID_DOWNLOAD, ID_AUTOR) ");
                    SQL.Append(" VALUES ");
                    SQL.Append("    (" + o.ID_DOWNLOAD.ToString() + ", " + o.ID_AUTOR.ToString() + ") ");

                    insertT_AUTOR_DOWNLOAD[index] = database.GetSqlStringCommand(SQL.ToString());

                    insertT_AUTOR_DOWNLOAD[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in insertT_AUTOR_DOWNLOAD)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Alterar[]

        public void alterar(T_AUTOR_DOWNLOAD[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] updateT_AUTOR_DOWNLOAD = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_AUTOR_DOWNLOAD o = obj[index];
                    SQL = new StringBuilder();
                    SQL.Append(" UPDATE T_AUTOR_DOWNLOAD SET ");
                    SQL.Append("    ID_DOWNLOAD = " + o.ID_DOWNLOAD.ToString() + " , ");
                    SQL.Append("    ID_AUTOR = " + o.ID_AUTOR.ToString() + " ");
                    SQL.Append(" WHERE ");
                    SQL.Append("     ID_DOWNLOAD = :ID_DOWNLOAD  ");

                    updateT_AUTOR_DOWNLOAD[index] = database.GetSqlStringCommand(SQL.ToString());

                    updateT_AUTOR_DOWNLOAD[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in updateT_AUTOR_DOWNLOAD)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
        
        #region Excluir
        public void excluir(T_AUTOR_DOWNLOAD obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" DELETE FROM T_AUTOR_DOWNLOAD ");
                SQL.Append(" WHERE ");
                SQL.Append("     ID_DOWNLOAD = " + obj.ID_DOWNLOAD.ToString());

                DbCommand deleteT_AUTOR_DOWNLOAD = database.GetSqlStringCommand(SQL.ToString());

                deleteT_AUTOR_DOWNLOAD.CommandText = SQL.ToString();

                database.ExecuteNonQuery(deleteT_AUTOR_DOWNLOAD, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region CONSULTAR
        public List<T_AUTOR_DOWNLOAD> consultar(int codigo)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT A.ID_DOWNLOAD,A.ID_AUTOR,B.FL_TIPO_AUTOR FROM T_AUTOR_DOWNLOAD A, T_AUTOR B WHERE ");
                SQL.Append("A.ID_AUTOR = B.ID_AUTOR AND ID_DOWNLOAD = " + codigo.ToString() + " ORDER BY B.FL_TIPO_AUTOR");

                DbCommand consultaT_AUTOR_DOWNLOAD = database.GetSqlStringCommand(SQL.ToString());
                consultaT_AUTOR_DOWNLOAD.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_AUTOR_DOWNLOAD, DALConnection.transaction);
                List<T_AUTOR_DOWNLOAD> lista = new List<T_AUTOR_DOWNLOAD>();
                T_AUTOR_DOWNLOAD obj = null;

                while (reader.Read())
                {
                    obj = new T_AUTOR_DOWNLOAD();
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_DOWNLOAD")))
                    {
                        obj.ID_DOWNLOAD = Int32.Parse(reader["ID_DOWNLOAD"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_AUTOR")))
                    {
                        obj.ID_AUTOR = Int32.Parse(reader["ID_AUTOR"].ToString());
                    }
                    lista.Add(obj);
                }
                reader.Dispose();
                consultaT_AUTOR_DOWNLOAD.Dispose();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
